package com.dddpeter.app.common;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

import org.apache.log4j.Logger;

import com.dddpeter.app.service.LoginService;

/** 
* @ClassName: DESCoder 
* @Description: DES编解码程序
* @author Peter Lee
* @date 2014年8月11日 下午1:57:23 
*  
*/
public class DESCoder {

	/**
	 * 
	 */
	private static final Logger LOG = Logger.getLogger(LoginService.class);
	private static final String KEY_ALGORITHM = "DESede";
	private static final String DEFAULT_CIPHER_ALGORITHM = "DESede/ECB/ISO10126Padding";
	private static final int KEY_SIZE = 168;
	private static final String SEED="FADGRFGRTHGHJJGJGFJFJHGDFSDFDR1131213131231";
	public static String getSeed() {
		return SEED;
	}

	/**
	 * 
	 * 
	 * @return byte[]
	 * @throws BusException 
	 */
	public static Key initSecretKey(String seed) throws BusException  {
		try{
		KeyGenerator keyPairGen = KeyGenerator.getInstance(KEY_ALGORITHM);
		SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
		secureRandom.setSeed(seed.getBytes("UTF-8"));
		keyPairGen.init(KEY_SIZE, secureRandom);
		SecretKey secretKey = keyPairGen.generateKey();
		DESedeKeySpec dks = new DESedeKeySpec(secretKey.getEncoded());
		SecretKeyFactory skf = SecretKeyFactory.getInstance(KEY_ALGORITHM);
		return skf.generateSecret(dks);
		}catch(Exception e){
			throw new BusException(e.getLocalizedMessage());
		}
	}

	/**
	 * 
	 * 
	 * @param data
	 * 
	 * @param key
	 * 
	 * @return byte[]
	 * @throws Exception
	 */
	public static byte[] encrypt(byte[] data, Key key) throws Exception {
		return encrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
	}

	/**
	 * 
	 * 
	 * @param data
	 * 
	 * @param key
	 * 
	 * @param cipherAlgorithm
	 * 
	 * @return byte[]
	 * @throws Exception
	 */
	public static byte[] encrypt(byte[] data, Key key, String cipherAlgorithm)
			throws Exception {

		Cipher cipher = Cipher.getInstance(cipherAlgorithm);
		cipher.init(Cipher.ENCRYPT_MODE, key);
		return cipher.doFinal(data);
	}

	/**
	 * 
	 * 
	 * @param data
	 * 
	 * @param key
	 * 
	 * @return byte[]
	 * @throws Exception
	 */
	public static byte[] decrypt(byte[] data, Key key) throws BusException {
		return decrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
	}

	/**
	 * 
	 * @param data
	 * 
	 * @param key
	 * 
	 * @param cipherAlgorithm
	 * 
	 * @return byte[]
	 * @throws BusException 
	 */
	public static byte[] decrypt(byte[] data, Key key, String cipherAlgorithm) throws BusException {

		Cipher cipher;
		try {
			cipher = Cipher.getInstance(cipherAlgorithm);
			cipher.init(Cipher.DECRYPT_MODE, key);
			return cipher.doFinal(data);
		} catch (Exception e) {
			throw new BusException(e.getLocalizedMessage());
		}
		
	}
	public  static void main(String[] args){
		byte[] tmppass;
		try {
			tmppass = "".getBytes("UTF-8");
			Key key = DESCoder.initSecretKey(DESCoder.getSeed());
			tmppass=DESCoder.encrypt("admin"
							.getBytes("UTF-8"),key);
			LOG.info(new String(tmppass));
			System.out.println(tmppass.toString());
			
		} catch (UnsupportedEncodingException e) {
			  
			LOG.error(e.getMessage());
		} catch (Exception e) {
			  
			LOG.error(e.getMessage());
		} catch (BusException e) {
			  
			LOG.error(e.getMessage());
		}
		
	}

}